//
//  AXFHeadView.m
//  AIXF
//
//  Created by Macx on 2017/7/9.
//  Copyright © 2017年 Macx. All rights reserved.
//

#import "AXFHeadView.h"
#import <SDCycleScrollView.h>
#import "AXFHomeSever.h"
#import "AXFHomeCycleImageModel.h"
#import "AXFHomeMiddleModel.h"
#import <SDImageCache.h>
#import "AXFIconButton.h"
#import <UIImageView+WebCache.h>
#import "AXFIconView.h"
@interface AXFHeadView ()<SDCycleScrollViewDelegate>
@property (weak, nonatomic) IBOutlet UIView *imageCycleView;

@property (weak, nonatomic) IBOutlet UIStackView *middleStackView;

@property (weak, nonatomic) IBOutlet UIImageView *focus1ImageView;
@property (weak, nonatomic) IBOutlet UIImageView *focus2Imageview;
@property (weak, nonatomic) IBOutlet UIImageView *focus3ImageView;
@property (weak, nonatomic) IBOutlet UIImageView *focus4ImageView;
@property (weak, nonatomic) IBOutlet UIStackView *bigPicStack;

@property (nonatomic,strong)NSArray<AXFHomeCycleImageModel *> *focusModelArray;
@property (nonatomic,strong)NSArray<AXFHomeMiddleModel *> *iconsModelArray;
@property (nonatomic,strong)NSArray<AXFHomeMiddleModel *> *activitiesModelArray;

@end

@implementation AXFHeadView


- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame])
    {
        [self setupUI];
    }
    
    return self;
}

- (void)awakeFromNib
{
    [super awakeFromNib];
    
    [self setupUI];
}


- (void)setupUI
{
    
    [[AXFHomeSever new] requestHomeFocusWith:^(NSDictionary *response) {
        
        //NSLog(@"%@",response);
        
        self.focusModelArray = [NSArray yy_modelArrayWithClass:[AXFHomeCycleImageModel class] json:response[@"focus"]];
        
        self.iconsModelArray = [NSArray yy_modelArrayWithClass:[AXFHomeMiddleModel class] json:response[@"icons"]];
        
        self.activitiesModelArray = [NSArray yy_modelArrayWithClass:[AXFHomeMiddleModel class] json:response[@"activities"]];
        
        dispatch_async(dispatch_get_main_queue(), ^{

            [self setupCycleImage];
            [self setupIconsView];
            [self setupFourBigPic];
        });
        
    } andFailBlock:^(NSError *error)
     {
        
    }];

}

// 图片轮播器功能
- (void)setupCycleImage
{
    NSMutableArray *imageArray = [NSMutableArray array];
    
    for (int  i = 0 ; i < self.focusModelArray.count; i++)
    {
        AXFHomeCycleImageModel *model = self.focusModelArray[i];
        
        [imageArray addObject:model.img];
    }
    
    SDCycleScrollView *cycleScrollView = [[SDCycleScrollView alloc] initWithFrame:self.imageCycleView.bounds];
    
    cycleScrollView.delegate = self;
    
    cycleScrollView.imageURLStringsGroup = imageArray;
    
    [self.imageCycleView addSubview:cycleScrollView];
}

//中间iconView
- (void)setupIconsView
{
    for (int i = 0; i < self.iconsModelArray.count; i++)
    {
        AXFHomeMiddleModel *model = self.iconsModelArray[i];
        
        AXFIconView *iconView = [AXFIconView new];
        
        iconView.imageUrl = model.img;
        
        iconView.name = model.name;
        
        [self.middleStackView addArrangedSubview:iconView];
        
        iconView.tag = i;
        
        iconView.userInteractionEnabled = YES;
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickIconView:)];
        
        [self.middleStackView addGestureRecognizer:tap];
        
    }
}

// 四张大图
- (void)setupFourBigPic
{
    [self.focus1ImageView sd_setImageWithURL:[NSURL URLWithString:self.activitiesModelArray[0].img]];
    [self.focus2Imageview sd_setImageWithURL:[NSURL URLWithString:self.activitiesModelArray[1].img]];
    [self.focus3ImageView sd_setImageWithURL:[NSURL URLWithString:self.activitiesModelArray[2].img]];
    [self.focus4ImageView sd_setImageWithURL:[NSURL URLWithString:self.activitiesModelArray[3].img]];
    self.focus1ImageView.userInteractionEnabled = YES;
    self.focus1ImageView.userInteractionEnabled = YES;
    self.focus1ImageView.userInteractionEnabled = YES;
    self.focus1ImageView.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickBigPicView:)];
    [self.bigPicStack addGestureRecognizer:tap];
    
}

- (void)clickBigPicView:(UITapGestureRecognizer *)tap
{
    CGPoint point = [tap locationInView:tap.view];
    
    //NSLog(@"%@",tap.view);
    
    NSInteger index = point.y  * 4 / self.bigPicStack.bounds.size.height;
    
    AXFHomeMiddleModel *model = self.activitiesModelArray[index];
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"postIconModel" object:model];
    
}

// 点击iconView实现跳转
- (void)clickIconView:(UITapGestureRecognizer *)tap
{
    CGPoint point = [tap locationInView:tap.view];
    
    NSLog(@"%@",tap.view);
    
    NSInteger index = point.x  * 4 / self.middleStackView.bounds.size.width;
    
    AXFHomeMiddleModel *model = self.iconsModelArray[index];
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"postIconModel" object:model];
}

//点击轮播器图片实现跳转
- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index
{
    AXFHomeCycleImageModel *model = self.focusModelArray[index];
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"postFocusModel" object:model];
}



@end
